package blitting 
{
	import assets.Asset;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * @author Charlie MacIsaac
	 */
	public class SpriteSheet {
		// PROPERTIES =============================================================================
		private var _sheet : Bitmap;
		private var _data : XML;
		
		private var _renderers : Vector.<SpriteSheetRenderer>;
		
		// GETTERS + SETTERS ======================================================================
		// CONSTRUCTOR ============================================================================
		public function SpriteSheet(sheet : Asset, data : Asset) {
			_sheet = sheet.content as Bitmap;
			_data = new XML(data.content);
			
			extractSpriteSheets();
		}
		
		// PUBLIC =================================================================================
		public function getRenderer(id : String) : SpriteSheetRenderer {
			for each (var spriteSheetRenderer : SpriteSheetRenderer in _renderers) {
				if (spriteSheetRenderer.id == id) return spriteSheetRenderer;
			}
			throw new Error("[SpriteSheetMaster.getRenderer] |ERROR| SpriteSheet [" + id + "] not found.");
		}
		
		// PRIVATE ================================================================================
		private function extractSpriteSheets() : void {
			var bitmapData : BitmapData;
			var origin : Point = new Point(0, 0);
			
			_renderers = new Vector.<SpriteSheetRenderer>();
			
			for each (var sheetData : XML in _data.sprite) {
				bitmapData = new BitmapData(sheetData.@width, sheetData.@height, true, 0x000000);
				bitmapData.copyPixels(_sheet.bitmapData, new Rectangle(sheetData.@x, sheetData.@y, sheetData.@width, sheetData.@height), origin);
				bitmapData.lock();
				_renderers.push(new SpriteSheetRenderer(bitmapData, sheetData));
			}
		}
		
		// LISTENERS ==============================================================================
	}
}